<?php

namespace app\api\controller\statistics;

use app\api\controller\Api;

use app\common\system\logic\DepartmentLogic;
use app\common\system\logic\UserLogic;
use function PHPSTORM_META\type;

class Dashboard extends Api
{
    public function index()
    {
        /**
         * 进店客资
         * 首次 = 非异业渠道
         * 转介绍 = 异业渠道
         * =============================================
         * 客资签约
         * 订单数 = 订单数
         * 签约金额 = 订单签约金额合计
         * 签约率 = 个人接收了多少咨询，产生的订单（小明接了10个咨询，定了一个，签约率=10%）
         * 门店签约总数 = 单个门店签约总单数（西郊店本周订单10个）
         * 门店签约率 = 门店本周接收了100个咨询。订单20单。（20%签约率）
         */
        $user = $this->request->user;
        $post = [];
        $post['range'] = 'month';

        $format = new \util\Format();
        $post['range'] = $format->dateRange($post['range']);
        $userLogic = new UserLogic();
        if ($user['user_type'] == 2) {
            $userIds = $userLogic->getDptUserIds($user['id']);
        } else {
            $userIds = [$user['id']];
        }

        // 获取门店id
        $dptLogic = new DepartmentLogic();
        $dptIds = $dptLogic->getUserDptIds($user['id']);
        $storeIds = $dptLogic->getStoreIds($dptIds);

        $result = [];
        // 进店客资统计
        $logic = new \app\common\customer\logic\CheckinLogic();
        $result['checkin'] = $logic->statistics($userIds, $post['range']);


        $logic = new \app\common\customer\logic\AllocateLogic();
        // 客资签约统计
        $result['sign'] = $logic->roi($user, $userIds, $storeIds, $post['range']);

        // type == customer 统计客资
        $result['customer'] = $logic->statistics($userIds, $post['range']);

        // type == order 
        $logic = new \app\common\order\logic\OrderLogic();
        $result['order'] = $logic->statistics($userIds, $post['range']);

        // type = suborder
        $logic = new \app\common\order\logic\SuborderLogic();
        $result['suborder'] = $logic->statistics($userIds, $post['range']);

        return success($result, '获取数据成功');
    }

    public function item()
    {
        $post = $this->request->post();
        $user = $this->request->user;

        $userLogic = new UserLogic();
        if ($user['user_type'] == 2) {
            $userIds = $userLogic->getDptUserIds($user['id']);
        } else {
            $userIds = [$user['id']];
        }

        // 获取门店id
        $dptLogic = new DepartmentLogic();
        $dptIds = $dptLogic->getUserDptIds($user['id']);
        $storeIds = $dptLogic->getStoreIds($dptIds);

        $type = $post['type'] ?? 'customer';

        $format = new \util\Format();
        $post['range'] = $format->dateRange($post['range']);

        $result = [];
        if ($type == 'customer') {
            $logic = new \app\common\customer\logic\CheckinLogic();
            $result['checkin'] = $logic->statistics($userIds, $post['range']);
        }

        // type == customer 统计客资
        if ($type == 'checkin') {
            $logic = new \app\common\customer\logic\AllocateLogic();
            $result['customer'] = $logic->statistics($userIds, $post['range']);
        }

        if ($type == 'sign') {
            $logic = new \app\common\customer\logic\AllocateLogic();
            $result['sign'] = $logic->roi($user, $userIds, $storeIds, $post['range']);
        }

        // type == order 
        if ($type == 'order') {
            $logic = new \app\common\order\logic\OrderLogic();
            $result['order'] = $logic->statistics($userIds, $post['range']);
        }

        // type = suborder
        $result['range'] = $post['range'];
        if ($type == 'suborder') {
            $logic = new \app\common\order\logic\SuborderLogic();
            $result['suborder'] = $logic->statistics($userIds, $post['range']);
        }

        return success($result, '获取数据成功');
    }
}
